﻿using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using WssUtils;


namespace JQMergeFramework
{
    public class UILoading:MonoBehaviour
    {
        
        
   
        
        public GameObject btnPlay;
        public Text loadingTips;
        public Text btnTxt;
        private AsyncOperation mAysncLoad;

        private float times = 0;
        
        private void Start()
        {
            //关闭多点触控
            Input.multiTouchEnabled = false;

            //屏幕设置
            Screen.sleepTimeout = SleepTimeout.NeverSleep;
            
            UIManager.Instance.AddBtnListener(this.transform);
            
            MergeManager.Instance.Init();

            if (DataManager.Instance.Lv > 0)
            {
                btnTxt.text = "继续游戏";
                DataManager.Instance.CurrentScore = TableConfig.Instance.GetNumericalCfg(DataManager.Instance.Lv).addScore;
            }
            else
            {
                DataManager.Instance.CurrentScore = 0;
                btnTxt.text = "开始游戏";
                
                //清除已经盖过的房子信息
                DataManager.Instance.ClearBuildCityIdList();
            }
            
            AudioManager.Instance.PlayBGM(AudioName.HomeBGM);
        }
        

        
        void OnBtnClick(Transform child)
        {
            AudioManager.Instance.Play(AudioName.Click);
            
            switch (child.name)
            {
                case "BtnPlayGame":
                    LoadGame();
                    break;
            }
        }
        
        
        void LoadGame () {
            btnPlay.SetActive(false);
            mAysncLoad = SceneManager.LoadSceneAsync("CityGame");
            mAysncLoad.allowSceneActivation = false;
        }

        private void Update() {
            if (mAysncLoad == null) return;
            if (mAysncLoad.progress >= .9f) {
                mAysncLoad.allowSceneActivation = true;     
            }

            times += Time.deltaTime;

            if (times < 1)
            {
                loadingTips.text = "加载中.";
            }
            else if (times < 2)
            {
                loadingTips.text = "加载中..";
            }
            else if (times < 3)
            {
                loadingTips.text = "加载中...";
            }
            else if (times > 4)
            {
                times = 0;
            }
        }
        
        private void OnDestroy()
        {
            DOTween.KillAll();
        }
    }
}